
<!DOCTYPE html>
<!--[if IEMobile 7 ]><html class="no-js iem7"><![endif]-->
<!--[if lt IE 9]><html class="no-js lte-ie8"><![endif]-->
<!--[if (gt IE 8)|(gt IEMobile 7)|!(IEMobile)|!(IE)]><!--><html class="no-js" lang="en"><!--<![endif]-->
<head>
  <meta charset="utf-8">
  <title>AFNetworking-2.0主项目 - ksnowlv</title>
  <meta name="author" content="律威">

  
  <meta name="description" content="AFNetworking-2.0主项目源代码文件组成： AFNetworking-2.0主项目类组成： AFNetworking 2.0相比2.0做了优化调整。 兼容NSURLSession，适配iOS7.
模块化，可以自由挑选你需要的模块。
实时推送。AFNetworking主项目并没有这个特性 &hellip;">
  

  <!-- http://t.co/dKP3o1e -->
  <meta name="HandheldFriendly" content="True">
  <meta name="MobileOptimized" content="320">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  
  <link rel="canonical" href="http://ksnowlv.github.io/blog/2014/09/02/afnetworking-2-dot-0">
  <link href="/favicon.png" rel="icon">
  <link href="/stylesheets/screen.css" media="screen, projection" rel="stylesheet" type="text/css">
  <link href="/atom.xml" rel="alternate" title="ksnowlv" type="application/atom+xml">
  <script src="/javascripts/modernizr-2.0.js"></script>
  <script src="//ajax.useso.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
  <script>!window.jQuery && document.write(unescape('%3Cscript src="./javascripts/lib/jquery.min.js"%3E%3C/script%3E'))</script>
  <script src="/javascripts/octopress.js" type="text/javascript"></script>
  <!--Fonts from Google"s Web font directory at http://google.com/webfonts -->
<link href="http://fonts.useso.com/css?family=PT+Serif:regular,italic,bold,bolditalic" rel="stylesheet" type="text/css">
<link href="http://fonts.useso.com/css?family=PT+Sans:regular,italic,bold,bolditalic" rel="stylesheet" type="text/css">

  
  <script type="text/javascript">
    var _gaq = _gaq || [];
     _gaq.push(['_setAccount', 'UA-39546615-2']);
    // _gaq.push(['_trackPageview']);

    // (function() {
    //   var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    //   ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    //   var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
    // })();

    _gaq.push(['_trackPageview']);

      (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src='http://droidyue-tools.qiniudn.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
      })();
  </script>


</head>

<body   >
  <header role="banner"><hgroup>
  <h1><a href="/">ksnowlv</a></h1>
  
    <h2>回顾过去,总结以往;立足现在,铭记当下;技术为主,笔记而已.</h2>
  
</hgroup>

</header>
  <nav role="navigation"><ul class="subscription" data-subscription="rss">
  <li><a href="/atom.xml" rel="subscribe-rss" title="subscribe via RSS">RSS</a></li>
  
</ul>
  
<form action="http://google.com/search" method="get">
  <fieldset role="search">
    <input type="hidden" name="q" value="site:ksnowlv.github.io" />
    <input class="search" type="text" name="q" results="0" placeholder="Search"/>
  </fieldset>
</form>
  
<ul class="main-navigation">
  <li><a href="/">博客首页</a></li>
  <li><a href="/blog/archives">文章列表</a></li>
  <!-- <li><a href="/about-me">关于我</a></li>
 -->
</ul>

</nav>
  <div id="main">
    <div id="content">
      <div>
<article class="hentry" role="article">
  
  <header>
    
      <h1 class="entry-title">AFNetworking-2.0主项目</h1>
    
    
      <p class="meta">
        








  


<time datetime="2014-09-02T19:18:00+08:00" pubdate data-updated="true">Sep 2<span>nd</span>, 2014</time>
        
         | <a href="#disqus_thread">Comments</a>
        
      </p>
    
  </header>


<div class="entry-content"><h4>AFNetworking-2.0主项目源代码文件组成：</h4>

<p><img src="/images/post/2014-09-02-afnetworking-2-dot-0/afnetworking_code_overview.png" alt="image" /></p>

<h4>AFNetworking-2.0主项目类组成：</h4>

<p><img src="/images/post/2014-09-02-afnetworking-2-dot-0/AFNetworking_class_overview.png" alt="image" /></p>

<h3><a href="https://github.com/AFNetworking/AFNetworking">AFNetworking 2.0</a>相比2.0做了优化调整。</h3>

<blockquote><ul>
<li>兼容NSURLSession，适配iOS7.</li>
<li>模块化，可以自由挑选你需要的模块。</li>
<li>实时推送。AFNetworking主项目并没有这个特性，而是放到子项目<a href="https://github.com/AFNetworking/AFRocketClient">AFRocketClient</a></li>
</ul>
</blockquote>

<p>中文译文为<a href="http://nshipster.cn/afnetworking-2/">http://nshipster.cn/afnetworking-2/</a></p>

<h4>版本支持情况</h4>

<table>
<thead>
<tr>
<th></th>
<th align="center">  AFNetworking 版本 &nbsp;&nbsp;&nbsp;&nbsp;</th>
<th align="center">   最低支持 iOS Target  &nbsp;&nbsp;&nbsp;&nbsp;</th>
<th align="center">     最低支持 OS X Target      &nbsp;&nbsp;&nbsp;&nbsp;</th>
<th align="center">     注意事项          &nbsp;&nbsp;&nbsp;&nbsp; </th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td align="center">          2.x      </td>
<td align="center">      iOS 6            </td>
<td align="center">           OS X 10.8          </td>
<td align="center"> Xcode 5 is required  </td>
</tr>
<tr>
<td></td>
<td align="center">          <a href="https://github.com/AFNetworking/AFNetworking/tree/1.x">1.x</a>      </td>
<td align="center">      iOS 5            </td>
<td align="center">         Mac OS X 10.7        </td>
<td align="center">                      </td>
</tr>
<tr>
<td></td>
<td align="center">         <a href="https://github.com/AFNetworking/AFNetworking/tree/0.10.x">0.10.x</a>    </td>
<td align="center">      iOS 4            </td>
<td align="center">         Mac OS X 10.6        </td>
<td align="center">                      </td>
</tr>
</tbody>
</table>


<h3><br>结构：<br></h3>

<h4>NSURLConnection</h4>

<ul>
<li><p><strong>AFURLConnectionOperation</strong></p>

<p>  <code>NSOperation</code>的子类，实现<code>NSURLConnectionDelegate</code>,
<code>NSURLConnectionDataDelegate</code>,控制<code>NSURLConnection</code>的创建，取消，
进行中，完成，并对外回调执行状态。</p></li>
<li><p><strong>AFHTTPRequestOperation</strong></p>

<p>   <code>AFURLConnectionOperation</code> 的子类，用于生成 <code>HTTP</code> 请求，可以区别可接受的和不可接受的状态码及内容类型。</p></li>
<li><p><strong>AFHTTPRequestOperationManager</strong>:</p>

<p>  继承 NSObject类，遵循<code>NSSecureCoding</code>, <code>NSCopying</code>协议，包装常见 HTTP web 服务操作的类，包括请求的创建，响应的序列化，网络状态监控，安全性。通过<code>NSOperationQueue</code>对象来调度<code>AFHTTPRequestOperation</code>对象。</p></li>
</ul>


<h4>小结：</h4>

<p>   1.AFURLConnectionOperation/AFHTTPRequestOperation及其子类共用享线程networkRequestThread的runloop,网络请求回调处理完后，准备好数据向上层调用者回调。</p>

<p>   2.使用状态机来控制NSOperation的状态切换，状态的改变通过在<code>setState</code>中通过KVO触发。</p>

<ul>
<li> AFOperationPausedState</li>
<li> AFOperationReadyState</li>
<li> AFOperationExecutingState</li>
<li> AFOperationFinishedState
默认为<code>AFOperationReadyState</code></li>
</ul>


<h4>NSURLSession支持 <em>(iOS 7 / Mac OS X 10.9)</em></h4>

<ul>
<li><p><strong>AFURLSessionManager</strong>：</p>

<p> 创建、管理基于 <code>NSURLSessionConfiguration</code> 对象的 <code>NSURLSession</code> 对象的类，也可以管理 session 的数据、下载/上传任务，实现 session 和其相关联的任务的 delegate 方法</p></li>
<li><strong>AFHTTPSessionManager</strong>：- <code>AFURLSessionManager</code> 的子类，包装常见的 HTTP web 服务操作，通过 <code>AFURLSessionManager</code>由<code>NSURLSession</code> 支持</li>
</ul>


<h4>小结：</h4>

<p>   目前还没有明确拒绝<code>NSURLConnection</code>,<code>iOS7</code>的<code>NSURLSession</code>是不是以后会逐步取代<code>NSURLConnection</code>？</p>

<h4>序列化(Serialization)</h4>

<ul>
<li><p><strong>&lt;AFURLRequestSerialization></strong></p>

<p>  符合这个协议的对象用于处理请求，它将请求参数转换为 query string 或是 entity body 的形式，并设置必要的 header。</p>

<ul>
<li><code>AFHTTPRequestSerializer</code>:http标准格式</li>
<li><code>AFJSONRequestSerializer</code>:json格式</li>
<li><code>AFPropertyListRequestSerializer</code>:属性列表格式</li>
</ul>
</li>
<li><p><strong>&lt;AFURLResponseSerialization></strong></p>

<p>  符合这个协议的对象用于验证、序列化响应及相关数据，转换为有用的形式，比如 JSON 对象、图像、甚至基于 Mantle 的模型对象</p>

<ul>
<li><code>AFHTTPResponseSerializer</code>：http标准格式</li>
<li><code>AFJSONResponseSerializer</code>:json格式</li>
<li><code>AFXMLParserResponseSerializer</code>:xml格式</li>
<li><code>AFXMLDocumentResponseSerializer</code> <em>(Mac OS X)</em></li>
<li><code>AFPropertyListResponseSerializer</code>:属性列表格式</li>
<li><code>AFImageResponseSerializer</code>:图片格式</li>
<li><code>AFCompoundResponseSerializer</code>:网络失败时的数据填充。</li>
</ul>
</li>
</ul>


<h4>小结:</h4>

<p>  主要是针对请求数据和响应数据格式的增强，提高易用性。</p>

<h4>Additional Functionality</h4>

<ul>
<li><code>AFSecurityPolicy</code>:https安全策略。</li>
<li><code>AFNetworkReachabilityManager</code>:网络状态监听。</li>
</ul>


<h4>UIKit 扩展支持(UIKit expansion)</h4>

<ul>
<li><code>AFNetworkActivityIndicatorManager</code>:新增自动开始或结束状态栏上的网络指示器。</li>
<li><code>UIImageView+AFNetworking</code>: 新增显示图片前剪裁或者加滤镜的功能。</li>
<li><code>UIButton+AFNetworking</code> (新增): 类似 UIImageView+AFNetworking，按钮的背景图从线上下载。</li>
<li><code>UIActivityIndicatorView+AFNetworking</code> (新增): 根据网络请求的状态自动开始或结束。</li>
<li><code>UIProgressView+AFNetworking</code> (新增): 自动跟踪某个请求的上传下载的进度。</li>
<li><code>UIWebView+AFNetworking</code> (新增): 支持网络请求的进度和内容转换。</li>
</ul>


<h4>小结:</h4>

<p>   提供一些和网络相关UI的扩展的支持,提高使用便捷性。</p>
</div>


  <footer>
    <p class="meta">
      
  

<span class="byline author vcard">Posted by <span class="fn">律威</span></span>

      








  


<time datetime="2014-09-02T19:18:00+08:00" pubdate data-updated="true">Sep 2<span>nd</span>, 2014</time>
      

<span class="categories">
  
    <a class='category' href='/blog/categories/ios-wang-luo/'>iOS-网络</a>
  
</span>


      
<DIV >
作者： <A href="/">律威</A> <BR>
出处： <A href="/">http://ksnowlv.gitcafe.com</A> 
<BR>原创文章，版权声明：自由转载-非商用-非衍生-保持署名
<a href= "http://creativecommons.org/licenses/by-nc-nd/3.0/deed.zh"> Creative Commons BY-NC-ND 3.0 </a>。 </DIV>


    </p>
<!--     
      Included file 'post/sharing.html' not found in _includes directory
     -->
    <p class="meta">
      
        <a class="basic-alignment left" href="/blog/2014/08/31/mac-xia-an-zhuang-pei-zhi-mysql/" title="Previous Post: mac下安装配置mysql">&laquo; mac下安装配置mysql</a>
      
      
        <a class="basic-alignment right" href="/blog/2014/09/02/ios-zhong-fan-she-de-ying-yong/" title="Next Post: iOS中反射的应用">iOS中反射的应用 &raquo;</a>
      
    </p>
  </footer>
</article>

  <section>
    <h1>Comments</h1>
    <div id="disqus_thread" aria-live="polite"><div id="disqus_thread"></div>
<script type="text/javascript">
var disqus_shortname = 'ksnowlv';
(function() {
    var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
    dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
    (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
    
</div>
  </section>

</div>

<aside class="sidebar">
  
    <section>
  <h1>关于我</h1>
  <p>律威,QQ:309235009 微信:ksnowlv</p>
  <p>手机单机游戏/手机网游封神online/手机阅读/腾讯iOS地图客户端&实时公交&开放API(地图&街景)/iPad/iPhone淘宝客户端及性能优化</p>
  <p>目前在银客做银客/简理财/...</p>
  <p>能做一点,是一点;不求大成,不求一万年;积少成多,水到渠成!</p>
</section>
<section>
  <h1>最近发布</h1>
  <ul id="recent_posts">
    
      <li class="post">
        <a href="/blog/2017/04/26/android-drawable/">Android drawable</a>
      </li>
    
      <li class="post">
        <a href="/blog/2017/04/22/iOS-xia-shi-yong-ffmpeg/">iOS下使用ffmpeg</a>
      </li>
    
      <li class="post">
        <a href="/blog/2015/08/19/ios-you-xian-hou-tai-ren-wu/">iOS有限后台任务</a>
      </li>
    
      <li class="post">
        <a href="/blog/2015/08/19/android-xia-jie-ya-suo/">Android下gzip数据流解压缩</a>
      </li>
    
      <li class="post">
        <a href="/blog/2015/08/19/facebook-infer-de-shi-yong/">FaceBook-infer的使用</a>
      </li>
    
  </ul>
</section>
<section>
  <h1>分类</h1>
    <ul id="category-list"><li><a href='/blog/categories/android-jie-ya-suo/'>Android-解压缩 (1)</a></li><li><a href='/blog/categories/androidduo-xian-cheng/'>Android多线程 (3)</a></li><li><a href='/blog/categories/androidcha-jian/'>Android插件 (1)</a></li><li><a href='/blog/categories/androidhui-tu-./'>Android绘图。 (1)</a></li><li><a href='/blog/categories/androidwang-luo/'>Android网络 (1)</a></li><li><a href='/blog/categories/git/'>git (6)</a></li><li><a href='/blog/categories/githubbo-ke/'>github博客 (8)</a></li><li><a href='/blog/categories/ios/'>iOS (6)</a></li><li><a href='/blog/categories/ios-block/'>iOS-block (1)</a></li><li><a href='/blog/categories/ios-bug/'>iOS-Bug (2)</a></li><li><a href='/blog/categories/ios-memory/'>iOS-Memory (4)</a></li><li><a href='/blog/categories/ios-push/'>iOS-push (1)</a></li><li><a href='/blog/categories/ios-ui/'>iOS-UI (2)</a></li><li><a href='/blog/categories/ios-you-hua/'>iOS-优化 (1)</a></li><li><a href='/blog/categories/ios-nei-cun/'>iOS-内存 (1)</a></li><li><a href='/blog/categories/ios-ji-chu/'>iOS-基础 (3)</a></li><li><a href='/blog/categories/ios-duo-xian-cheng/'>iOS-多线程 (3)</a></li><li><a href='/blog/categories/ios-cun-chu/'>iOS-存储 (1)</a></li><li><a href='/blog/categories/ios-an-quan/'>iOS-安全 (1)</a></li><li><a href='/blog/categories/ios-wang-luo/'>iOS-网络 (13)</a></li><li><a href='/blog/categories/ios-jie-ya-suo/'>iOS-解压缩 (1)</a></li><li><a href='/blog/categories/ios-she-ji-mo-shi/'>iOS-设计模式 (1)</a></li><li><a href='/blog/categories/ios-diao-shi/'>iOS-调试 (1)</a></li><li><a href='/blog/categories/iosyou-hua/'>iOS优化 (1)</a></li><li><a href='/blog/categories/iosji-chu/'>iOS基础 (2)</a></li><li><a href='/blog/categories/iosyin-pin/'>iOS音频 (1)</a></li><li><a href='/blog/categories/mac/'>Mac (4)</a></li><li><a href='/blog/categories/mac-duo-xian-cheng/'>Mac-多线程 (1)</a></li><li><a href='/blog/categories/nginx/'>nginx (3)</a></li><li><a href='/blog/categories/php/'>php (3)</a></li><li><a href='/blog/categories/python/'>python (12)</a></li><li><a href='/blog/categories/swift/'>swift (11)</a></li><li><a href='/blog/categories/uml/'>UML (8)</a></li><li><a href='/blog/categories/xcode/'>Xcode (11)</a></li><li><a href='/blog/categories/chan-pin-she-ji/'>产品设计 (2)</a></li><li><a href='/blog/categories/gong-ju/'>工具 (2)</a></li><li><a href='/blog/categories/shu-ju-ku/'>数据库 (2)</a></li><li><a href='/blog/categories/sheng-huo-pian-duan/'>生活片段 (10)</a></li><li><a href='/blog/categories/zhi-ye-sheng-ya/'>职业生涯 (7)</a></li><li><a href='/blog/categories/du-shu/'>读书 (2)</a></li></ul>
</section>
<section>
  <h1>外部链接</h1>
    <ul id="外部链接">
    	<li><a href="https://developer.apple.com">苹果开发者中心</a> </li>
    	<li><a href="http://stackoverflow.com/questions/tagged/ios">stackoverflow</a></li>
    	<li><a href="http://ksnowlv.blog.163.com/blog/#m=0">ksnowlv网易博客</a></li>
	</ul>
</section>

  
</aside>


    </div>
  </div>
  <footer role="contentinfo"><p>
  Copyright &copy; 2017 - 律威 -
  <span class="credit">Powered by <a href="http://octopress.org">Octopress</a></span>

  <script type="text/javascript">var cnzz_protocol = (("https:" == document.location.protocol) ? " https://" : " http://");document.write(unescape("%3Cspan id='cnzz_stat_icon_1253538110'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "v1.cnzz.com/z_stat.php%3Fid%3D1253538110%26online%3D1' type='text/javascript'%3E%3C/script%3E"));</script>

  <script type="text/javascript">
var _bdhmProtocol = (("https:" == document.location.protocol) ? " https://" : " http://");
document.write(unescape("%3Cscript src='" + _bdhmProtocol + "hm.baidu.com/h.js%3F4c1727a94ea20751730e8a556dbdbf82' type='text/javascript'%3E%3C/script%3E"));
</script>

</p>



</footer>
  

<script type="text/javascript">
      var disqus_shortname = 'ksnowlv';
      
        
        // var disqus_developer = 1;
        var disqus_identifier = 'http://ksnowlv.github.io/blog/2014/09/02/afnetworking-2-dot-0/';
        var disqus_url = 'http://ksnowlv.github.io/blog/2014/09/02/afnetworking-2-dot-0/';
        var disqus_script = 'embed.js';
      
    (function () {
      var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
      dsq.src = 'http://' + disqus_shortname + '.disqus.com/' + disqus_script;
      (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
    }());
</script>








</body>
</html>
